﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DCTAccess.Business;
using DCTLib.Common;
using DCTAccess.Business.Administrator;
using DCTAccess.Business.Member;

public partial class AdminZone_AdminUpdate : System.Web.UI.Page
{
    public string Message
    {
        get { return lblError.Text; }
        set { lblError.Text = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //lblFullName.Text = AppSessionInfo.CurrentAdministrator.ID.ToString();
        BindData();
    }

    protected void BindData()
    {
        Administrator admin = AdministratorController.GetAdministratorByID(AppCookieInfo.AdminID);
        if (admin != null)
        {
            txtFullName.Text = admin.FullName;
            txtUsername.Text = admin.UserName;
            txtBirthday.Text = admin.Birthday;
            txtRegDate.Text = admin.RegDate;
            txtMobile.Text = admin.Mobile;
            txtAddress.Text = admin.Address;
            txtEmail.Text = admin.Email;
        }

    }

    protected void btnSavePass_Click(object sender, EventArgs e)
    {
        if (ValidateData())
        {
            Administrator adv = GetAdminForChangePass();
            Administrator admin = AdministratorController.GetAdministratorByID(adv.ID);

            if (MD5Helper.GetMD5Hash(txtOldPassword.Text) == admin.HashedPassword)
            {
                if (AdministratorController.ChangePassword(adv))
                {
                    Message = AppMessages.UPDATE_SUCCESSFULLY;
                }
                else
                {
                    Message = AppMessages.CANNOT_UPDATE;
                }
            }
            else
            {
                Message = AppMessages.NOT_CORRECT_OLD_PASSWORD;
            }
        }
        else
        {
            Message = AppMessages.NOT_CORRECT_OLD_PASSWORD;
        }
    }

    protected Administrator GetAdminForChangePass()
    {
        Administrator ad = new Administrator();
        int id = AppSessionInfo.CurrentAdministrator.ID;
        ad.ID = id;
        ad.HashedPassword = MD5Helper.GetMD5Hash(txtNewPassword.Text);
        return ad;
    }

    private Administrator GetAminForChange()
    {
        Administrator admin = new Administrator();
        admin.ID = AppSessionInfo.CurrentAdministrator.ID;
        admin.FullName = txtFullName.Text;
        admin.Address = txtAddress.Text;
        admin.Email = txtEmail.Text;
        admin.Role = int.Parse(this.ddlRole.SelectedValue);
        return admin;
    }

    private bool ValidateSaveChangeData()
    {
        bool validated = true;
        if (ValidationHelper.IsEmpty(txtFullName))
        {
            lblInfoMsg.Text = AppMessages.RequireField("Full Name") + "<br>";
            validated = false;
        }

        if (ValidationHelper.IsEmpty(txtEmail))
        {
            lblInfoMsg.Text = AppMessages.RequireField("Email") + "<br>";
            validated = false;
        }

        if (this.ddlRole.SelectedIndex == 0)
        {
            lblInfoMsg.Text = AppMessages.RequireField("Role") + "<br>";
            validated = false;
        }

        return validated;
    }

    public bool ValidateData()
    {
        bool validated = true;
        if (ValidationHelper.IsEmpty(txtOldPassword.Text.Trim()))
        {
            Message += AppMessages.RequireField("Old Password") + "<br>";
            validated = false;
        }
        if (ValidationHelper.IsEmpty(txtNewPassword.Text.Trim()))
        {
            Message += AppMessages.RequireField("New Password") + "<br>";
            validated = false;
        }
        if (ValidationHelper.IsEmpty(txtReNewPassword.Text.Trim()))
        {
            Message += AppMessages.RequireField("Confirm New Password") + "<br>";
            validated = false;
        }
        else if (txtNewPassword.Text.Trim() != txtReNewPassword.Text.Trim())
        {
            Message += AppMessages.MustMatch("New Password", "Confirm New Password") + "<br>";
            validated = false;
        }
        return validated;
    }

    protected void btnSaveInfo_Click(object sender, EventArgs e)
    {
        if (ValidateSaveChangeData())
        {
            Administrator admin = GetAminForChange();
            if (AdministratorController.Update(admin))
            {
                lblInfoMsg.Text = AppMessages.UPDATE_SUCCESSFULLY;
                BindData();
            }
            else
            {
                lblInfoMsg.Text = AppMessages.CANNOT_UPDATE;
            }
        }
    }
}
